package com.drool.mediaplayer.Adapters;

import com.drool.mediaplayer.R;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;

import android.net.Uri;
import android.provider.MediaStore.Audio;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class SortedAlbumAdapter extends CursorAdapter{

	public static final int SORT_BY_DATE_ADDED = 1;
	
	private int sortType;
	
	public SortedAlbumAdapter(Context context, Cursor c, int mySortType) {
		super(context, c);
		sortType = mySortType;
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		// TODO Auto-generated method stub
		Uri albums = Audio.Albums.EXTERNAL_CONTENT_URI;
		
		/*
		Cursor c = Activity.managedQuery(albums,
					new String []{Audio.Albums._ID,
					Audio.Albums.ALBUM,Audio.Albums.ARTIST,
					Audio.Albums.FIRST_YEAR, Audio.Albums.NUMBER_OF_SONGS},
					Audio.Albums._ID + " = ?",new String []{cursor.getString(0)},
					null);
		*/
		
		TextView albumTxt = (TextView)view.findViewById(R.id.text1);
		TextView artistTxt = (TextView)view.findViewById(R.id.text2);
		TextView yearTxt = (TextView)view.findViewById(R.id.text3);
		TextView numSongsTxt = (TextView)view.findViewById(R.id.text4);
		
		ImageView albumArt = (ImageView) view.findViewById(R.id.albumImage);
		
		
		albumTxt.setText(cursor.getString(cursor.getColumnIndex(Audio.Albums.ALBUM)));
		artistTxt.setText("Artist: " + cursor.getString(cursor.getColumnIndex(Audio.Albums.ARTIST)));
		if (cursor.getString(cursor.getColumnIndex(Audio.Albums.FIRST_YEAR)) != null){
			yearTxt.setText("Year: " + cursor.getString(cursor.getColumnIndex(Audio.Albums.FIRST_YEAR)));
		}
		else
			yearTxt.setText("Year: Unknown");
		
		numSongsTxt.setText("Songs: " + cursor.getString(cursor.getColumnIndex(Audio.Albums.NUMBER_OF_SONGS)));
		
		
		if (cursor.getString(cursor.getColumnIndex(Audio.Albums.ALBUM_ART)) != null	){
			albumArt.setImageURI(Uri.parse(cursor.getString(cursor.getColumnIndex(Audio.Albums.ALBUM_ART))));
		}
		else
			albumArt.setImageURI(null);
		
		
		
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		LayoutInflater inflater = LayoutInflater.from(context);
		View v = inflater.inflate(R.layout.layadapter, parent, false);
		bindView(v, context, cursor);
		return v;
	}
	
}
